import type { Component } from 'vue';

export function isVueComponent(obj: unknown): obj is Component {
  if (!obj || typeof obj !== 'object') return false;

  const candidate = obj as Record<string, any>;
  
  // Vue 3 核心标识
  if (candidate.__isVue === true) return true;
  
  // 组合式/选项式 API
  if (typeof candidate.setup === 'function' || 
      typeof candidate.render === 'function' || 
      typeof candidate.template === 'string') {
    return true;
  }

  // 异步组件
  if (typeof candidate.__asyncLoader === 'function') return true;

  // 通过 defineComponent 创建的组件
  if ('__vccOpts' in candidate) return true;

  // SFC 组件
  if (typeof candidate.__file === 'string' && candidate.__file.endsWith('.vue')) {
    return true;
  }

  return false;
}

/**
 * 判断是否是 VNode 对象
 */
export function isVNode(obj: unknown): obj is VNode {
  return !!obj && typeof obj === 'object' && (obj as any).__v_isVNode === true;
}

/**
 * 判断是否是 Vue 组件或 VNode
 */
export function isVueComponentOrVNode(obj: unknown): obj is Component | VNode {
  return isVueComponent(obj) || isVNode(obj);
}